#!/usr/bin/env bash
### BEGIN INIT INFO
# Provides:          tengine
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:
# Short-Description: starts Tengine
# Description:       Starts Tengine
### END INIT INFO

NAME=tengine
BIN=/usr/sbin/tengine
PID=#PID_FILE#
CMD="${BIN}"

. /lib/lsb/init-functions

test_nginx_config() {
        if "${CMD}" -t >/dev/null 2>&1; then
                return 0
        else
                "${CMD}" -t
                return $?
        fi
}

case "$1" in
        test)
                log_daemon_msg "Testing ${NAME} configuration" "tengine" || true
                if test_nginx_config; then
                        log_end_msg 0 || true
                else
                        log_end_msg 1 || true
                        exit $?
                fi
        ;;
        start)
                log_daemon_msg "Starting tengine" "tengine" || true
                if start-stop-daemon --start --quiet --oknodo --pidfile $PID --exec $CMD; then
                        log_end_msg 0 || true
                else
                        log_end_msg 1 || true
                fi
        ;;
        stop)
                log_daemon_msg "Stopping tengine" "tengine" || true
                if start-stop-daemon --stop --quiet --oknodo --pidfile $PID; then
                        log_end_msg 0 || true
                else
                        log_end_msg 1 || true
                fi
        ;;
        reload)
                log_daemon_msg "Reloading tengine" "tengine" || true
                if start-stop-daemon --stop --signal 1 --quiet --oknodo --pidfile $PID --exec $CMD; then
                        log_end_msg 0 || true
                else
                        log_end_msg 1 || true
                fi
        ;;
        restart)
                log_daemon_msg "Restarting tengine" "tengine" || true
                if start-stop-daemon --start --quiet --oknodo --pidfile $PID --exec $CMD; then
                        log_end_msg 0 || true
                else
                        log_end_msg 1 || true
                fi
        ;;
        status)
                status_of_proc -p $PID $BIN $NAME && exit 0 || exit $?
        ;;
        *)
                log_action_msg "Usage: /etc/init.d/tengine {start|stop|reload|restart|test|status}" || true
                exit 1
esac

exit 0